<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>充值页面</title>
    <style>
    </style>
</head>

<body>
    <table width="100%" height="100%" border="0" cellpadding="1" cellspacing="0" class="TitleColor">
        <tr>
            <td width="15%" valign="top" bgcolor="#E1E1E1" height="100%">
                <div id='leftpage'>
            </td>

            <td width="85%" valign="top">
                <table width="800" border="0" cellpadding="0" cellspacing="0" align="center">
                    <tr>
                        <!-- 添加充值选项 -->
                        <td>
                            <h2>充值选项</h2>
                            <form id="rechargeForm" action="recharge.php" method="post">
                                <label for="rechargeType">充值类型:</label>
                                <select id="rechargeType" name="rechargeType">
                                    <option value="kcoin">K 币充值</option>
                                    <option value="rmb">RMB 充值</option>
                                </select><br>
                                <label for="amount">充值金额:</label>
                                <input type="number" id="amount" name="amount" min="1" required><br>
                                <input type="button" value="充值" onclick="startRecharge()">
                            </form>
                            <div id="paymentVoucher">
                                <label for="pVou">支付凭证:</label>
                                <input type="text" id="pVou" name="pVou">
                            </div>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td bgcolor="#E1E1E1" align="center">
                <div id='totalpage'></div>
            </td>
            <td height="60" align="center">
                <div id='footerpage'></div>
            </td>
        </tr>
    </table>
</body>
<script>
    function loadScript(c, d) { var b = document.createElement("script"); b.type = "text/javascript"; var e = sessionStorage.getItem(c); e ? (b.textContent = e, d(b)) : fetch(c).then(function (a) { return a.text() }).then(function (a) { b.textContent = a; sessionStorage.setItem(c, a); d(b); location.reload() })["catch"](function (a) { console.error("Error loading the script:", a) }) };
    loadScript("tu_0.js", function (script) { document.head.appendChild(script); });
</script>
<script type="text/javascript" language="javascript">
    if (sessionStorage.getItem("tu_0.js")) {		// 读取记忆是否已保存js文件
        loadHtml("left.htm", "leftpage");
        loadHtml("total.htm", "totalpage");
        loadHtml("footer.htm", "footerpage");
    }

    var first = 0;
    function fun() {
        if (first == 0) {
            first = 1;
            setTimeout(function () { myTimer("cookie", 0, 0, 0, 0) }, 0);		//自动发送post指,只有打开页面的时候，才发送一次送一次
        }
    }
    setInterval("fun()", 100);			// 周期性调用函数 每隔100毫秒调用一次	

    function myTimer(type, arr) {
        var phpName = "index.php";
        var xhr = postPhp(phpName);
        xhr.onreadystatechange = function () {		//2.注册回调函数
            var array_name = txtUnzip(xhr);
            if (array_name != null) {
                if (type == "cookie") {
                    cookiemyFunction(array_name)	// 返回的信息在html页面显
                }
                if (type == "Cread") {
                    myFunction(array_name)	// 返回的信息在html页面显
                }
                if (type == "submitk") {
                    submitkFunction(array_name)	// 返回的信息在html页面显
                }
                if (type == "submitr") {
                    submitrFunction(array_name)	// 返回的信息在html页面显
                }
            }
        }

        if (type == "cookie") {
            txtZip(xhr, "t:ncki");
        }
        if (type == "Cread") {
            // txtZip(xhr, "t:nare");
        }
        if (type == "submitk") {
            // 获取充值金额
            const amount = document.getElementById('amount').value;
            var voucher = document.getElementById('pVou').value;
            txtZip(xhr, "t:rchk&mony:" + amount + "&pvou:" + voucher, 1);
        }
        if (type == "submitr") {
            // 获取充值RMB
            const amount = document.getElementById('amount').value;
            txtZip(xhr, "t:rchr&mony:" + amount, 1);
        }
    }
    function cookiemyFunction(arr) {
        switch (arr.result) {
            case "ok":
                myTimer("Cread");
                document.getElementById("totalSpace").textContent = showSpace(arr.totd, arr.usdd);
                break;
            default:
                alert('请先登录后，再操作！');
                window.location.href = "user.htm";
                return false;
        }
    }
    function startRecharge() {
        const rechargeType = document.getElementById('rechargeType').value;
        const amount = document.getElementById('amount').value;

        if (rechargeType === 'rmb') {
            // 调用后端接口获取支付宝支付链接
            const xhr = new XMLHttpRequest();
            xhr.open('POST', 'get_alipay_url.php', true);
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    const response = JSON.parse(xhr.responseText);
                    if (response.success) {
                        // 跳转到支付宝支付页面
                        window.location.href = response.alipay_url;
                    } else {
                        alert('获取支付链接失败，请稍后重试');
                    }
                }
            };
            myTimer("submitr");
        } else if (rechargeType === 'kcoin') {
            myTimer("submitk");
        }
    }
    function myFunction(arr) {
    }

    function submitkFunction(arr) {
        switch (arr.result) {
            case "ok":
                alert('充值成功！');
                break;
            default:
                alert('充值失败！:' + arr.result);
                break;
        }
    }
    function submitrFunction(arr) {
        switch (arr.result) {
            case "ok":
                alert('充值成功！');
                break;
            default:
                alert('充值失败！:' + arr.result);
                break;
        }
    }

</script>

</html>